package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQeury;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {

    @Autowired
    private IScenicCommentRepository commentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference
    private IScenicService scenicService;

    @Override
    public ScenicComment commentAdd(ScenicComment scenicComment, UserInfo userInfo) {
        scenicComment.setUserId(userInfo.getId());
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
        scenicComment.setUsername(userInfo.getNickname());
        scenicComment.setLevel(userInfo.getLevel());
        scenicComment.setCreateTime(new Date());
        commentRepository.insert(scenicComment);
        scenicService.increaseCommentNum(scenicComment.getScenicId());
        return scenicComment;
    }

    @Override
    public Page<ScenicComment> query(ScenicCommentQeury qo) {
        Query query = new Query();
        query.addCriteria(new Criteria().andOperator(Criteria.where("scenicId").is(qo.getScenicId()),Criteria.where("type").is(ScenicComment.TYPE_COMMON)));
        long count = mongoTemplate.count(query, ScenicComment.class);
        if (count == 0){
            return Page.empty();
        }
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        query.with(pageable);
        List<ScenicComment> scenicComments = mongoTemplate.find(query, ScenicComment.class);
        /*for (ScenicComment scenicComment : scenicComments) {
            Query q = new Query();
            q.addCriteria(new Criteria().andOperator(Criteria.where("type").is(1),
                    Criteria.where("refCommentId").is(scenicComment.getRefCommentId()),
                    Criteria.where("scenicId").is(qo.getScenicId())));
            List<ScenicComment> refScenicComments = mongoTemplate.find(q, ScenicComment.class);
            scenicComment.setRefScenicComments(refScenicComments);
        }*/
        return new PageImpl<ScenicComment>(scenicComments,pageable,count);
    }

    @Override
    public ScenicComment refCommentAdd(ScenicComment scenicComment, UserInfo userInfo) {
        scenicComment.setCreateTime(new Date());
        scenicComment.setUserId(userInfo.getId());
        scenicComment.setUsername(userInfo.getNickname());
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
        commentRepository.insert(scenicComment);//新增评论

        ScenicComment comment = commentRepository.findById(scenicComment.getRefCommentId()).get();
        List<ScenicComment> refScenicComments = comment.getRefScenicComments();
        refScenicComments.add(scenicComment);

        //将评论加到点评中
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(scenicComment.getRefCommentId()));
        mongoTemplate.updateFirst(query, Update.update("refScenicComments",refScenicComments),ScenicComment.class);
        return scenicComment;
    }
}